﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    public class WinzipWrapper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="winzipExecutablePath"></param>
        /// <param name="srcPath"></param>
        /// <param name="tgtPath"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool ZipFile(string winzipExecutablePath, string srcPath, string tgtPath, string password)
        {
            bool flag = false;
            Process proc=new Process();
            ProcessStartInfo startInfo=new ProcessStartInfo();
            startInfo.FileName = winzipExecutablePath;
            string args = "-min -a";
            if(!string.IsNullOrEmpty(password))
            {
                args += " -s\"" + password + "\"";
            }
            args += string.Format(" \"{0}\" \"{1}\"", tgtPath, srcPath);
            startInfo.Arguments = args;
            proc.StartInfo = startInfo;
            proc.Start();
            proc.WaitForExit();
            flag = true;
            return flag;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="winzipExecutableFilePath"></param>
        /// <param name="srcPath"></param>
        /// <param name="tgtPath"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool UnzipFile(string winzipExecutableFilePath, string srcPath, string tgtPath, string password)
        {
            bool flag = false;
            Process proc = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = winzipExecutableFilePath;
            string args = "-min -e -o";
            if (!string.IsNullOrEmpty(password))
            {
                args += " -s\"" + password + "\"";
            }
            args += string.Format(" \"{0}\" \"{1}\"", tgtPath, srcPath);
            startInfo.Arguments = args;
            proc.StartInfo = startInfo;
            proc.Start();
            proc.WaitForExit();
            flag = true;
            return flag;
        }
    }
}
